﻿using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tool.Helper
{

    public class MSWordHelper
    {
        private Application wordApp;

        public Document wordDoc;
        public Microsoft.Office.Interop.Word.Table table;
        public Paragraph paragraph;


        public MSWordHelper()
        {
            wordApp = new Application();
            wordDoc = null;
        }
        /// <summary>
        /// 打开WORD
        /// </summary>
        /// <param name="filePath"></param>
        public void OpenDocument(string filePath)
        {
            wordDoc = wordApp.Documents.Open(filePath);
        }


        public Range GetRange()
        {
            return wordDoc.Content;
        }
        /// <summary>
        /// 创建WORD
        /// </summary>
        public void CreateDocument()
        {
            wordDoc = wordApp.Documents.Add();
        }
        /// <summary>
        /// 获取WORD中的表格列表
        /// </summary>
        /// <returns></returns>
        public Tables GetTables()
        {
            return wordDoc.Tables;
        }
        /// <summary>
        /// 设置当前表格并返回结果
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public bool SetTable(int index)
        {
            bool result = false;
            if (index <= wordDoc.Tables.Count)
            {
                table = GetTables()[index];
                result = true;
            }
            return result;
        }
        /// <summary>
        /// 返回表格最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxRowCount()
        {
            int maxRowCount = 0;
            if (table != null)
            {
                maxRowCount = table.Rows.Count;
            }
            return maxRowCount;
        }
        /// <summary>
        /// 返回表格最大列数
        /// </summary>
        /// <returns></returns>
        public int GetMaxColumuCount()
        {
            int maxColumeCount = 0;
            if (table != null)
            {
                maxColumeCount = table.Columns.Count;
            }
            return maxColumeCount;
        }

        /// <summary>
        ///判断表格单元格是否存在
        /// </summary>
        /// <param name="row"></param>
        /// <param name="clume"></param>
        /// <returns></returns>
        public bool IsExists(int row, int colume)
        {
            bool result = false;
            try
            {
                Range cellRange = table.Cell(row, colume).Range;
                if (cellRange != null)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                return result;
            }
            return result;
        }
        /// <summary>
        /// 获取单元格值
        /// </summary>
        /// <param name="row"></param>
        /// <param name="colume"></param>
        /// <returns></returns>
        public string GetCellValue(int row, int colume)
        {
            string result = "";
            if (IsExists(row, colume))
            {
                result = table.Cell(row, colume).Range.Text;
            }
            return result;
        }
        /// <summary>
        /// 设置单元格值
        /// </summary>
        /// <param name="row"></param>
        /// <param name="colume"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetCellValue(int row, int colume, string value)
        {
            bool result = false;
            if (IsExists(row, colume))
            {
                table.Cell(row, colume).Range.Text = value;
                result = true;
            }
            return result;
        }

        /// <summary>
        /// 获取段落列表
        /// </summary>
        /// <returns></returns>
        public Paragraphs GetParagraphs()
        {
            return wordDoc.Paragraphs;
        }
        /// <summary>
        /// 返回段落总数
        /// </summary>
        /// <returns></returns>
        public int GetParagraphCount()
        {
            return wordDoc.Paragraphs.Count;
        }
        /// <summary>
        /// 返回指定段落，并指定当前段落
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Paragraph GetParagraph(int index)
        {
            if (index > 0 && index <= GetParagraphCount())
            {
                paragraph = wordDoc.Paragraphs[index];
                return paragraph;
            }
            return null;
        }
        /// <summary>
        /// 添加段落
        /// </summary>
        /// <returns></returns>
        public Paragraph AddParagraph()
        {
            paragraph = wordDoc.Paragraphs.Add();
            return paragraph;
        }
        /// <summary>
        /// 添加段落
        /// </summary>
        /// <returns></returns>
        public Paragraph AddParagraph(string value)
        {
            paragraph = wordDoc.Paragraphs.Add();
            paragraph.Range.Text = value;
            InsertParagraphAfter();
            return paragraph;
        }

        public bool FindReplace(string findStr,string replaceStr)
        {
            var range = GetRange();
            Find find = range.Find;
            find.ClearFormatting();
            find.Text = findStr;
            find.Replacement.ClearFormatting();
            find.Replacement.Text = replaceStr;
            find.Forward = true; // 向前查找
            find.Wrap = WdFindWrap.wdFindContinue; // 继续查找直到文档末尾
            find.Format = false;
            find.MatchCase = false;
            find.MatchWholeWord = false;
            find.MatchWildcards = false;
            find.MatchSoundsLike = false;
            find.MatchAllWordForms = false;
            return find.Execute(Replace: WdReplace.wdReplaceOne, Forward: true, Wrap: WdFindWrap.wdFindContinue);
        }

        public void InsertParagraphAfter()
        {
            paragraph.Range.InsertParagraphAfter();

        }
        public void InsertParagraphBefore()
        {
            paragraph.Range.InsertParagraphBefore();
        }
        /// <summary>
        /// 返回指定段落的文本
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public string GetParagraphText(int index)
        {
            if (index > 0 && index <= GetParagraphCount())
            {
                return wordDoc.Paragraphs[index].Range.Text;
            }
            return "";
        }
        /// <summary>
        /// 设置指定段落的文本
        /// </summary>
        /// <param name="index"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetParagraphText(int index, string value)
        {
            bool result = false;
            if (index > 0 && index <= GetParagraphCount())
            {
                wordDoc.Paragraphs[index].Range.Text = value;
                result = true;
            }
            return result;
        }
        public void SetParagraphText(Paragraph pa, string value)
        {
            pa.Range.Text = value;
            //    InsertParagraphAfter();
        }

        /// <summary>
        /// 保存WORD
        /// </summary>
        /// <param name="filePath"></param>
        public void SaveDocument(string filePath)
        {
            wordDoc.SaveAs(filePath);
        }
        /// <summary>
        /// 关闭WORD
        /// </summary>
        public void CloseDocument()
        {
            wordDoc?.Close();
        }

        public void Quit()
        {
            CloseDocument();
            wordApp.Quit();
        }
    }
}
